package com.cqc.web;

import com.cqc.service.impl.DocumentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/documents")
public class DocumentController {

    private final DocumentServiceImpl documentServiceImpl;

    @Autowired
    public DocumentController(DocumentServiceImpl documentServiceImpl) {
        this.documentServiceImpl = documentServiceImpl;
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            // 假设你有一个生成唯一ID的方法
            String id = generateUniqueId();
            return new ResponseEntity<>(documentServiceImpl.indexDocument(id, file), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>("Error during upload: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private String generateUniqueId() {
        // 实现生成唯一ID的逻辑
        return java.util.UUID.randomUUID().toString();
    }
}